/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.webhomesites.locacao.presenter;

import br.com.webhomesites.locacao.model.BuscarManutencaoModel;
import br.com.webhomesites.locacao.model.ManutencaoDAO;
import br.com.webhomesites.locacao.model.ManutencaoModel;
import br.com.webhomesites.locacao.presenter.BuscarManutencaoPresenterActionListeners.BuscarActionListener;
import br.com.webhomesites.locacao.presenter.BuscarManutencaoPresenterActionListeners.BuscarFocusListener;
import br.com.webhomesites.locacao.presenter.BuscarManutencaoPresenterActionListeners.CampoKeyListener;
import br.com.webhomesites.locacao.view.BuscarManutencaoView;
import br.com.webhomesites.padrao.utils.Strategy;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Webhome
 */
public class BuscarManutencaoPresenter {

    BuscarManutencaoView buscarManutencaoView;
    ManutencaoModel manutencaoModel;
    ManutencaoDAO manutencaoDAO;
    BuscarManutencaoModel buscarManutencaoModel;
    ManutencaoPresenter manutencaoPresenter;
    List<ManutencaoModel> listaManutencaoModel = new ArrayList<>();
    public final Strategy BUSCAR_STRATEGY = new BuscarStrategy();
    public final Strategy POR_CODIGO_STRATEGY = new CodigoStrategy();
    public final Strategy POR_NOMECLIENTE_STRATEGY = new NomeEmpresaManutencaoStrategy();
    public final Strategy POR_DATASAIDA_STRATEGY = new DataStrategy();
    public final Strategy BUSCADOR_MANUTENCAO_VIEW = new ManutencaoViewStrategy();
    private Strategy operacao = BUSCAR_STRATEGY;
    private Strategy tipoBusca;
    private Strategy buscador;
    DecimalFormat df = new DecimalFormat("###,###,###.00");
    DecimalFormat df2 = new DecimalFormat("###,###,###.0");
    DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

    public BuscarManutencaoPresenter(BuscarManutencaoView buscarManutencaoView, BuscarManutencaoModel buscarManutencaoModel, ManutencaoDAO manutencaoDAO, ManutencaoModel manutencaoModel, ManutencaoPresenter manutencaoPresenter) {

        this.buscarManutencaoView = buscarManutencaoView;
        this.buscarManutencaoModel = buscarManutencaoModel;
        this.manutencaoDAO = manutencaoDAO;
        this.manutencaoModel = manutencaoModel;
        this.manutencaoPresenter = manutencaoPresenter;

    }

    public void setUpViewListeners() {
        buscarManutencaoView.setBtnBuscarActionListener(new BuscarActionListener(this));
        buscarManutencaoView.setCodigoFocusListener(new BuscarFocusListener(this));
        buscarManutencaoView.setNomeFocusListener(new BuscarFocusListener(this));
        buscarManutencaoView.setDataInicialFocusListener(new BuscarFocusListener(this));
        buscarManutencaoView.setDataFinalFocusListener(new BuscarFocusListener(this));
        buscarManutencaoView.setMascaraListener(new CampoKeyListener(this));

    }

    public void updateModelFromView() {
        buscarManutencaoModel.setCodigo(buscarManutencaoView.getComboCodigo());
        buscarManutencaoModel.setNomeCliente(buscarManutencaoView.getComboNomeCliente());
        buscarManutencaoModel.setDataInicialSaida(buscarManutencaoView.getDataInicialSaida());
        buscarManutencaoModel.setDataFinalSaida(buscarManutencaoView.getDataFinalSaida());
    }

    public void updateViewFromModel() {
        if (buscarManutencaoModel != null) {
            buscarManutencaoView.setComboCodigo(Long.toString(buscarManutencaoModel.getCodigo()));
            buscarManutencaoView.setComboNomeCliente(buscarManutencaoModel.getNomeCliente());
            //buscarManutencaoView.setTxtDataInicialSaida(buscarManutencaoModel.getDataInicialSaida());
            //buscarManutencaoView.setTxtDataFinalSaida(buscarManutencaoModel.getRg());            
        } else {
            //buscarManutencaoView.clearFields();
        }
    }

    public void novoBuscarManutencao() {
        getTipoBusca().execute();

        System.out.println("Executando a busca");

    }

    public void createView() {

        this.setUpViewListeners();
        
        buscarManutencaoView.packAndShow();
    }

    public void setOperacao(Strategy operacao) {
        this.operacao = operacao;
    }

    public Strategy getOperacao() {
        return operacao;
    }

    public Strategy getTipoBusca() {
        return tipoBusca;
    }

    public void setTipoBusca(Strategy tipoBusca) {
        this.tipoBusca = tipoBusca;
    }

    public Strategy getBuscador() {
        return buscador;
    }

    public void setBuscador(Strategy buscador) {
        this.buscador = buscador;
    }

    public BuscarManutencaoModel getBuscarManutencaoModel() {
        return buscarManutencaoModel;
    }

    public void setBuscarManutencaoModel(BuscarManutencaoModel buscarManutencaoModel) {
        this.buscarManutencaoModel = buscarManutencaoModel;
    }

    public BuscarManutencaoView getBuscarManutencaoView() {
        return buscarManutencaoView;
    }

    public void setBuscarManutencaoView(BuscarManutencaoView buscarManutencaoView) {
        this.buscarManutencaoView = buscarManutencaoView;
    }

    public ManutencaoDAO getManutencaoDAO() {
        return manutencaoDAO;
    }

    public void setManutencaoDAO(ManutencaoDAO manutencaoDAO) {
        this.manutencaoDAO = manutencaoDAO;
    }

    public ManutencaoModel getManutencaoModel() {
        return manutencaoModel;
    }

    public void setManutencaoModel(ManutencaoModel manutencaoModel) {
        this.manutencaoModel = manutencaoModel;
    }

    public ManutencaoPresenter getManutencaoPresenter() {
        return manutencaoPresenter;
    }

    public void setManutencaoPresenter(ManutencaoPresenter manutencaoPresenter) {
        this.manutencaoPresenter = manutencaoPresenter;
    }
    
    private class BuscarStrategy implements Strategy {

        @Override
        public void execute() {
            novoBuscarManutencao();
        }
    }

    private class CodigoStrategy implements Strategy {

        @Override
        public void execute() {
            listaManutencaoModel.clear();            
            manutencaoModel = manutencaoDAO.getById(buscarManutencaoView.getComboCodigo());
            listaManutencaoModel.add(manutencaoModel);
            getBuscador().execute();
        }
    }

    private class NomeEmpresaManutencaoStrategy implements Strategy {

        @Override
        public void execute() {
            listaManutencaoModel = manutencaoDAO.findByNomeEmpresaManutencao(buscarManutencaoView.getComboNomeCliente());
            System.out.println("Buscando pelo nome");
            getBuscador().execute();
        }
    }

    private class DataStrategy implements Strategy {

        @Override
        public void execute() {
            try {
                listaManutencaoModel = manutencaoDAO.findByIntervaloData((Date) formatter.parse(buscarManutencaoView.getDataInicialSaida()), (Date) formatter.parse(buscarManutencaoView.getDataFinalSaida()));
            } catch (ParseException ex) {
                Logger.getLogger(BuscarManutencaoPresenter.class.getName()).log(Level.SEVERE, null, ex);
            }
            getBuscador().execute();
        }
    }

    private class ManutencaoViewStrategy implements Strategy {

        @Override
        public void execute() {
            manutencaoPresenter.setOperacao(manutencaoPresenter.UPDATE_STRATEGY);
            if (tipoBusca == POR_CODIGO_STRATEGY) {
                manutencaoPresenter.setListaManutencao(listaManutencaoModel);
                manutencaoPresenter.updateTableManutencaoFromBusca(listaManutencaoModel); 
                manutencaoPresenter.updateTableitemFromManutencaoModel(manutencaoModel);
                manutencaoPresenter.setManutencaoModel(manutencaoModel);
                manutencaoPresenter.updateViewManutencaoFromModel();
            } 
            else if (tipoBusca==POR_NOMECLIENTE_STRATEGY || tipoBusca==POR_DATASAIDA_STRATEGY) {
                manutencaoPresenter.setListaManutencao(listaManutencaoModel);
                manutencaoPresenter.updateTableManutencaoFromBusca(listaManutencaoModel);
            }
        }
    }
}
